﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Views_module_account : System.Web.UI.UserControl
{
    CustomerBus cusBus = new CustomerBus();
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack){
            checkCookie();
            
        }
        
    }
    public void checkCookie()
    {
        if (Request.Cookies["RememberMe"] != null)
        {
            HttpCookie cookieRememberMe = Request.Cookies.Get("RememberMe");
            txtEmail.Text = cookieRememberMe.Values["UserName"].ToString();
            txtPassword.Attributes.Add("Value", cookieRememberMe.Values["Password"].ToString());

            if (cusBus.CheckLogin(txtEmail.Text.Trim(), txtPassword.Text.Trim()))
            {
                Customer cus = new Customer();
                cus = cusBus.getCustomerByEmail(txtEmail.Text.Trim());
                Session["username"] = txtEmail.Text.Trim();
                Session["first_name"] = cus.First_name.Trim();
                Session["customer_group_id"] = cus.Customer_group_id;
                if (cartBus.carts.listcart.Count > 0)
                {
                    Response.Redirect("~//order.aspx?step=2");
                }
                else
                {
                    Response.Redirect("~//default.aspx?action=cart_home");
                }

            }
        }
        else
        {
            Session["username"] = "";
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        lbError.Visible = false;
        string email = txtEmail.Text;
        string pass = txtPassword.Text;
        if (cusBus.CheckLogin(email, pass))
        {
            Customer cus = new Customer();
            cus = cusBus.getCustomerByEmail(email);
            Session["username"] = txtEmail.Text;
            Session["first_name"] = cus.First_name;
            Session["customer_group_id"] = cus.Customer_group_id;
            if (chkRemember.Checked == true)
            {

                HttpCookie cookieRememberMe = new HttpCookie("RememberMe");
                cookieRememberMe = new HttpCookie("RememberMe");
                cookieRememberMe.Values.Add("UserName", txtEmail.Text.Trim());
                cookieRememberMe.Values.Add("Password", txtPassword.Text.Trim());
                cookieRememberMe.Values.Add("CheckBox", "True");
                cookieRememberMe.Expires = DateTime.Now.AddDays(30);
                Response.Cookies.Add(cookieRememberMe);
                
            }
            else
            {
                //Request.Cookies["RememberMe"].Values["username"] = null;
                //Response.Write("order" + Request.Cookies["RememberMe"].Values["username"]);
            }

        }else
            {
                lbError.Visible = true;
                Session["username"] = "";
            }
        
    }
    protected void btnLogout_Click(object sender, EventArgs e)
    {
        Session["username"] = null;
        //HttpCookie cookie = Request.Cookies.Get("RememberMe");
        Request.Cookies["RememberMe"].Values["username"] = null;
        //Response.Write("order" + Request.Cookies["RememberMe"].Values["username"]);
        Response.Redirect("default.aspx");
    }
}